import React,{PureComponent} from 'react'
import {View,Button,SafeAreaView,Text,StyleSheet,TextInput,ScrollView,Dimensions,TouchableOpacity} from 'react-native'
import color from "../../../widget/color";
import RefreshListView, {RefreshState} from "react-native-refresh-list-view";
import MinePayCell from '../../../widget/mine/MinePayCell'
import MinePayDetailScene from './MinePayDetailScene'
import api from "../../../api/api";
import method from "../../../api/method";
import {fetchGetRequest} from "../../../api/NetworkTool";
import RefreshList from "../../../widget/multystateview/RefreshList";

const {width} = Dimensions.get('window')

type Props = {
    navigation:any,
}
type State = {
    data: Array<Object>,
    refreshState: number,
}

export default class MineUnUseCouponscene extends PureComponent<Props,State>{
    static navigationOptions = ({navigation}: any) => ({
        headerTintColor:'white',
        headerStyle:{
            backgroundColor:color.globalMainColor
        },
    });
    constructor(props) {
        super(props)
        this.state = {
            data: [],
            refreshState: RefreshState.Idle,
        }
    }
    componentDidMount() {
        this.requestNetData()
    }
    requestOrderData = (res) => {

        let dataList = res.orders.map((info) => {
            return {
                course:info.course,
                payStatus: info.payStatus,
                payment: info.payment,
                price:info.price,
                orderSn:info.orderSn,
                id:info.id,
            }
        })

        return dataList
    }

    requestNetData = (resolve) => {

        let params = {
            status:'paid',
        }
        fetchGetRequest(api.getOrderList, method.get,params)
            .then(res => {
                let dataList = this.requestOrderData(res)
                if (dataList.length > 0) {
                    this._listRef.setData(dataList);
                } else {
                    this._listRef.setData([]);
                }

                resolve();//调用该方法停止刷新

            })
            .catch(err => {
                this._listRef.setError();
                resolve();//调用该方法停止刷新
            })
    }

    //加载更多
    _loadMore() {
        this._listRef.addData([]);
    }
    //custon ui
    keyExtractor = (item: Object, index: number) => {
        return index + ''
    }
    renderCell = ({item}: any) => {
        return (
            <MinePayCell
                onPress={()=>{this.onCellPaySelected(item)}} info={item}
            />
        )
    }
    onCellPaySelected(info) {
        this.props.navigation.navigate('MinePayDetailScene',{orderId:info.id})
    }
    render(){
        return (
            <View style={{flex:1}}>
                <RefreshList
                    ref={(list)=> this._listRef = list}
                    onPullRelease={(resolve)=>this.requestNetData(resolve)}
                    onEndReached={()=> this._loadMore()}
                    renderItem={this.renderCell}
                    ItemHeight={200}
                    ItemSeparatorComponent={()=><View style={{width:width,height:6,backgroundColor:'#F9F9F9'}}/>}
                />
            </View>
        )
    }
}